package com.example.lenovo.weiyi;

import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.com.ruanmeng.demon.CommonM;
import com.com.ruanmeng.demon.NameListM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.Params;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import loopview.LoopView;
import loopview.OnItemSelectedListener;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 发起咨询
 */
public class FaQiZiXunActivity extends BaseActivity {

    @BindView(R.id.ed_fqzx)
    EditText edFqzx;
    @BindView(R.id.tv_hf_name)
    TextView tvHfName;
    ArrayList<String> temp_listname = new ArrayList<>();
    ArrayList<NameListM.UserInfosBean> Temp_Name = new ArrayList<>();
    @BindView(R.id.view_line_a)
    View viewLineA;
    private String name_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fa_qi_zi_xun);
        ButterKnife.bind(this);
        ChangLayTitle("发起咨询");
        LayBack();
        init();
    }

    public void init() {
    }

    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.FaBuZiXun, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(FaQiZiXunActivity.this, "uid"));
        mRequest.add("receiverId", name_id);
        mRequest.add("title", edFqzx.getText().toString());
        CallServer.getRequestInstance().add(FaQiZiXunActivity.this, 0, mRequest,
                new CustomHttpListener(FaQiZiXunActivity.this, true, CommonM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        CommonM model = ((CommonM) data);
                        Params.fqzu = 1;
                        finish();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        CommonUtil.showToask(FaQiZiXunActivity.this, obj.getString("msg"));
                    }
                }, true, true);
    }

    /**
     * 康复师列表
     */
    private void getNameData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.nameList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(FaQiZiXunActivity.this, "uid"));
        CallServer.getRequestInstance().add(FaQiZiXunActivity.this, 0, mRequest,
                new CustomHttpListener(FaQiZiXunActivity.this, true, NameListM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        NameListM model = ((NameListM) data);
                        Temp_Name.clear();
                        temp_listname.clear();
                        Temp_Name.addAll(model.getUserInfos());
                        for (int i = 0; i < Temp_Name.size(); i++) {
                            temp_listname.add(Temp_Name.get(i).getUserName());
                        }
                        if (temp_listname.size() > 0) {
                            ShowSelectname();
                        }
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        if ("101".equals(obj.getString("msgcode"))) {
                            CommonUtil.showToask(FaQiZiXunActivity.this, obj.getString("msg"));
                        }
                    }
                }, true, true);
    }

    public void doClick(View v) {
        switch (v.getId()) {
            case R.id.li_choose_name:
//                if (temp_listname.size() == 0) {
                    getNameData();
//                } else {
//                    ShowSelectname();
//                }
                break;
            case R.id.bt_fq_sure:
                if (TextUtils.isEmpty(tvHfName.getText().toString())) {
                    CommonUtil.showToask(FaQiZiXunActivity.this, "请选择回复人！");
                    return;
                }
                if (TextUtils.isEmpty(edFqzx.getText().toString())) {
                    CommonUtil.showToask(FaQiZiXunActivity.this, "请输入您的问题！");
                    return;
                }
                getData();
                break;
        }

    }

    private void ShowSelectname() {
        // 加载布局
        LayoutInflater inflater = LayoutInflater
                .from(FaQiZiXunActivity.this);
        View v = inflater.inflate(R.layout.pop_choose, null);
        LoopView loopView = (LoopView) v
                .findViewById(R.id.loopView);
        View view_opo = (View) v
                .findViewById(R.id.view_opo);
        TextView tv_cancel = (TextView) v
                .findViewById(R.id.tv_cancel);
        TextView tv_poap_title = (TextView) v
                .findViewById(R.id.tv_poap_title);
        tv_poap_title.setText("请选择回复人");
        // 创建popupwindow对话框
        final PopupWindow pop = new PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setFocusable(true);// 必须有，因为要获得焦点进行输入
        // 设置此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 点击其他除了popupwindow对话框的地方就可使popupwindow对话框消失
        pop.setOutsideTouchable(true);
        if (Build.VERSION.SDK_INT >= 24) {
            int[] a = new int[2];
            viewLineA.getLocationInWindow(a);
            pop.showAtLocation((FaQiZiXunActivity.this).getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, a[1] + viewLineA.getHeight());
        } else {
            pop.showAsDropDown(viewLineA);
        }
        //设置是否循环播放
        loopView.setNotLoop();
        //滚动监听
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                Log.d("debug", "Item " + index);
                tvHfName.setText(temp_listname.get(index));
                name_id = Temp_Name.get(index).getUserInfoId();
            }
        });
        //设置原始数据
        loopView.setItems(temp_listname);
        //设置初始位置
        loopView.setInitPosition(2);
//        //设置字体大小
//        loopView.setTextSize(30);

        view_opo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });
        tv_cancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                pop.dismiss();
                tvHfName.setText("");
                name_id = "";

            }
        });
    }
}
